from django.contrib.auth import get_user_model
from django.db import models

from shortuuidfield import ShortUUIDField


class Category(models.Model):
    name = models.CharField(verbose_name="分类名", max_length=20, unique=True)

    class Meta:
        ordering = ('id',)
        db_table = 'post_category'
        verbose_name = "文章分类表"
        verbose_name_plural = verbose_name


class Tag(models.Model):
    name = models.CharField(verbose_name="标签名", max_length=20, unique=True)

    class Meta:
        ordering = ('id',)
        db_table = 'post_tag'
        verbose_name = "文章标签表"
        verbose_name_plural = verbose_name


class Post(models.Model):
    pid = ShortUUIDField(primary_key=True)
    title = models.CharField(verbose_name="标题", max_length=100)
    desc = models.CharField(verbose_name="标题", max_length=300)
    content = models.TextField()
    thumbnail = models.URLField()
    like = models.IntegerField("喜欢", default=0)

    category = models.ForeignKey(Category, on_delete=models.DO_NOTHING, related_name='posts')
    tags = models.ManyToManyField(Tag, related_name='posts')
    author = models.ForeignKey(get_user_model(), on_delete=models.CASCADE)

    is_recom = models.BooleanField("推荐", default=False)
    is_del = models.BooleanField("删除", default=False)
    pub_time = models.DateTimeField("发布日期", auto_now_add=True)

    class Meta:
        ordering = ('-pub_time',)
        db_table = 'post'
        verbose_name = "文章表"
        verbose_name_plural = verbose_name
